package cn.imu.interceptor;

import cn.imu.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    //访问接口之前调用，返回true放行，返回false拦截住
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("LoginInterceptor.preHandle()前-----------------------");
        //获得session
        HttpSession session = request.getSession();
        //获得session里的user对象
        User user = (User) session.getAttribute("user");
        System.out.println("LoginInterceptor.preHandle()后-----------------------");
        if (user==null){

            return false;
        }else {
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
}
